package com.demo.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.pojo.User;
import com.demo.service.UserService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

@RestController
@RequestMapping(value = "/crud")
@Slf4j
@Api(value = "用户接口")
public class UserController {


    @Autowired
    private UserService userservice;

    @GetMapping("/hello")
    public String show(){
        return "hello";
    }


    @RequestMapping("/findUser")
    public List<User> findUser() {
        return userservice.findUser();
    }

    @ApiOperation(value="获取用户", notes="获取用户")
    @ResponseBody
    @PostMapping("/ListUser")
    public IPage<User>  ListUser(@RequestBody JSONObject json){
        Integer pageNum = json.getInteger("pageNum");
        Integer pageSize = json.getInteger("pageSize");
        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userIPage = userservice.ListUser(page);
         return  userIPage;
    }

    @ApiOperation(value="测试接口api", notes="根据cid获取客服信息，this is test api")
    @RequestMapping("/ListUserByname")
    @ResponseBody
    public List<User> ListUserByname(String name){
        return userservice.findByName(name);
    }


    @RequestMapping(value = "/delete")
    public String delete(int id) {
        int result = userservice.delete(id);
        if (result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user) {
        int result = userservice.Update(user);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }

    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public User insert(User user) {
        return userservice.insertUser(user);
    }


    @RequestMapping("/insertUser")
    public User insertUser(String userName) {
        return userservice.insert(userName);
    }


    public static void main(String[] args) {
        String sb = "bdsajjds";
        String substring = sb.substring(0, 2);
        System.out.println(substring);

        String  aa ="/ssop/img/vendor/logo/f659bc46325e408ca352c6ad1065f72c.png";
        String replace = aa.replace("/data/ssop/misc/server/webapps/", "");
        System.out.println(replace);
        /*
       String[] strs = new String[]{"bfa4296282fd4464b7bf25dfbeee318b ","53e02b90ec084c71a9ea8c8801e819dc"};
         String ss = "bfa4296282fd4464b7bf25dfbeee318b";
        List<String> goodsIdsList = Lists.newArrayList();
        for (int i = 0; i< strs.length; i++) {
            String goodId = strs[i].trim();
            goodsIdsList.add(goodId);
        }
        if (!goodsIdsList.contains(ss)) {
            System.out.printf("不包含");
        }else {
            System.out.printf("包含");
        }*/

    }
    @RequestMapping(value = "/doExport")
    public void exportExcelData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb =   new   HSSFWorkbook();
//建立新的sheet对象（excel的表单）
        HSSFSheet sheet=wb.createSheet(  "成绩表"  );
        //在sheet里创建第二行
        HSSFRow row2=sheet.createRow(  0  );
        //创建单元格并设置单元格内容
        row2.createCell(  0  ).setCellValue(  "姓名"  );
        row2.createCell(  1  ).setCellValue(  "班级"  );
        row2.createCell(  2  ).setCellValue(  "笔试成绩"  );
        row2.createCell(  3  ).setCellValue(  "机试成绩"  );
//输出Excel文件
        OutputStream output=response.getOutputStream();
        response.reset();
        response.setHeader(  "Content-disposition"  ,   "attachment; filename=details.xls"  );
        response.setContentType(  "application/msexcel"  );
        wb.write(output);
        output.close();
    }
}